package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.FieldContentVo;
import com.zb.entity.ZjNetworkFieldContent;
import com.zb.mapper.ZjNetworkFieldContentMapper;
import com.zb.service.ZjNetworkFieldContentService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 网点字段内容表，当zj_network_field表字段type=3/4时，单选/多选内容存入此表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-10-18
 */
@Service
public class ZjNetworkFieldContentServiceImpl extends ServiceImpl<ZjNetworkFieldContentMapper, ZjNetworkFieldContent> implements ZjNetworkFieldContentService {

    // 保存、修改、删除字段内容
    @Override
    public void save(Long custAccId, Long networkFieldId, List<FieldContentVo> contents, List<Long> ids) {
        if (null != ids && !ids.isEmpty()) {
            this.removeByIds(ids);
        }
        if (null == contents || contents.isEmpty()) {
            return;
        }
        Assert.isTrue(contents.stream().map(FieldContentVo::getVal).toList().size() == contents.stream().map(FieldContentVo::getVal).distinct().toList().size(), "单选/多选内容已重复！");
        List<ZjNetworkFieldContent> adds = new ArrayList<>();
        List<ZjNetworkFieldContent> updates = new ArrayList<>();
        ZjNetworkFieldContent content = null;
        for (FieldContentVo vo : contents) {
            content = new ZjNetworkFieldContent();
            BeanUtil.copyProperties(vo, content);
            if (null != content.getId()) {
                updates.add(content);
                continue;
            }
            content.setId(IdWorker.getId());
            content.setCustAccId(custAccId);
            content.setNetworkFieldId(networkFieldId);
            adds.add(content);
        }
        if (!adds.isEmpty()) this.saveBatch(adds);
        if (!updates.isEmpty()) this.updateBatchById(updates);
    }

    // 根据服务商字段ID获取单选/多选内容
    @Override
    public List<ZjNetworkFieldContent> getByNetworkFieldIds(List<Long> networkFieldIds) {
        if (null == networkFieldIds || networkFieldIds.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjNetworkFieldContent> wrapper = new QueryWrapper<>();
        wrapper.select("id", "network_field_id", "val");
        wrapper.in("network_field_id", networkFieldIds);
        return this.list(wrapper);
    }

    @Override
    public List<ZjNetworkFieldContent> getByNetworkFieldId(Long networkFieldId) {
        List<Long> networkFieldIds = new ArrayList<>();
        networkFieldIds.add(networkFieldId);
        return getByNetworkFieldIds(networkFieldIds);
    }

    // 根据服务商字段ID删除
    @Override
    public void deleteBatchByNetworkFieldIds(List<Long> networkFieldIds) {
        if (CollectionUtils.isEmpty(networkFieldIds)) {
            return;
        }
        QueryWrapper<ZjNetworkFieldContent> wrapper = new QueryWrapper<>();
        wrapper.in("network_field_id", networkFieldIds);
        this.remove(wrapper);
    }
}
